﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GCELibrary.Current;
 
using System.Linq.Dynamic;
using System.Linq.Expressions;
using ZF.BLL.Linq;
using ZF.BLL;
using ZF.WS.Admin;


namespace ZF.BLL.Admin
{
    public class fitup : ZF.BLL.Page.MMPage
    {
        public override ModuleType Module
        {
            get { return ModuleType.Info; }
            protected set
            {
                base.Module = value;
            }
        }
        public virtual string ListPage { get { return string.Format("{0}/info/infolist.aspx", AppKey.SiteKey.ManageDirectory); } }
        public virtual string ManagePage { get { return string.Format("/info/pub.aspx", AppKey.SiteKey.ManageDirectory); } }
        public virtual string CTitle { get { return "分类信息"; } }
    }
    public partial class fitupList : fitup
    {
   
        [PageArgument(Key = "tid",CanNull=false )]
        public int TypeId { get; set; }
  
        protected void Page_Load(object sender, EventArgs e)
        {
       
            if (!IsPostBack)
            {
                InitControls();
                View();
            }
        }
        public void InitControls()
        {
            
        }
        public void View()
        {
            ListBind();
        }
        /// <summary>
        /// 列表绑定
        /// </summary>
        public void ListBind()
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {

                List<Expression<Func<Model.I_Fitup, bool>>> filterList = new List<Expression<Func<Model.I_Fitup, bool>>>();
                IQueryable<Model.I_Fitup> selector = dc.I_Fitup;
                if (this.CurrentUser.UserType != UserType.Admin)
                    filterList.Add(p => p.MemberId == this.CurrentUser.MemberId);

             
                filterList.Add(p => p.TypeId == this.TypeId);

                foreach (var filter in filterList)
                {
                    selector = selector.Where(filter);
                }
                 
         
                var selPage = selector.Where(this.gvList).OrderBy(this.gvList.SortExpression)
                        .Skip(gvList.PageIndex * gvList.PageSize).Take(gvList.PageSize);

                this.gvList.RowCount = selector.Count();

                this.gvList.DataSource = selPage.ToList();
                this.gvList.DataBind();


                

            }
        }
        /// <summary>
        /// 排序事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void gvListSorting(object sender, GridViewSortEventArgs e)
        {
            ListBind();
        }
        /// <summary>
        /// 换页事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void gvListPageChanging(object sender, GridViewPageEventArgs e)
        {
            this.gvList.PageIndex = e.NewPageIndex;
            ListBind();
        }
        public void Delete(params int[] idArr)
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {
                bool isSuccess = true;
                try
                {
                    dc.I_Fitup.DeleteAllOnSubmit(
                       dc.I_Fitup.Where(p => idArr.Contains(p.FitupId)));
                    dc.SubmitChanges();
                }
                catch (Exception ex)
                {
                    isSuccess = false;
                    throw SiteInfo.WarnFactory.CreateWarn(ex);
                }
                string msgKey = this.TypeId==1?"装修日志":"最新优惠";
                if (isSuccess)
                    WarnFactory.CreateWarn(string.Format("删除{0}成功", msgKey), WarnIcon.Success, WarnLink.Current).Alert();

            }
        }
        public void Delete(object sender, EventArgs e)
        {
            var bc = (IButtonControl)sender;
            if (bc.CommandArgument == "select")
            {
                var idArrTemp = this.gvList.GetCheckKeys();
                if (idArrTemp.Length == 0)
                    throw SiteInfo.WarnFactory.CreateWarn("未选择任何记录");
                var idArr = idArrTemp.Select(p => p.Convert<int>()).ToArray();
                Delete(idArr);
            }
            else
            {
                Delete(bc.CommandArgument.Convert<int>());
            }
        }
        public void Search(object sender, EventArgs e)
        {
            //this.gvList.RemoveAllFilterExpression();
            //if (this.tbTitle.Text.EmptyString() != null)
            //    this.gvList.AddFilterExpression("House.Title.Contains(@0)", this.tbTitle.Text);
            //if (this.tbHouseNo.Text.EmptyString() != null)
            //    this.gvList.AddFilterExpression("House.HouseId=@0", this.tbHouseNo.Text.Convert<int>()-10000);
            //if (this.ddlHouseType.SelectedValue != "-1")
            //    this.gvList.AddFilterExpression("House.HouseType=@0", this.ddlHouseType.SelectedValue);
            //if (this.ddlHouseStatus.SelectedValue != "-1")
            //    this.gvList.AddFilterExpression("House.Status=@0", this.ddlHouseStatus.SelectedValue.Convert<int>());
            //if (this.tbEndTime1.Text.EmptyString() != null)
            //    this.gvList.AddFilterExpression("House.AddTime<=@0", this.tbEndTime1.Text.Convert<DateTime>());
            //if (this.tbEndTime2.Text.EmptyString() != null)
            //    this.gvList.AddFilterExpression("House.AddTime>=@0", this.tbEndTime2.Text.Convert<DateTime>());

            ListBind();
        }
        public void AllList(object sender, EventArgs e)
        {
            this.gvList.RemoveAllFilterExpression();
            ListBind();
        }



        public void Oper(object sender, EventArgs e)
        {
            var bc = (IButtonControl)sender;
            var idArrTemp = this.gvList.GetCheckKeys().Select(p=>p.Convert<int>()).ToArray();
            if (idArrTemp.Length == 0)
                throw SiteInfo.WarnFactory.CreateWarn("未选择任何记录");
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {
                var list = dc.I_Info.Where(p => idArrTemp.Contains(p.InfoId)).ToList();
                switch (bc.CommandArgument)
                {
                    case "TopUp":
                        list.ForEach(p => { 
                            p.IsTop = true;                        
                        });
                        break;
                    case "TopDown":
                        list.ForEach(p =>
                        {
                            p.IsTop = false; 
                        });
                        break;
                    //case "CancelDiscount":
                    //    list.ForEach(p =>
                    //    {
                    //        p.Discount = 0;
                    //    });
                    //    break;
                    //case "Del":
                    //    list.ForEach(p =>
                    //    {
                    //        p.IsDel = true;
                    //    });
                    //    break; 
                    //case "IsRecommend":
                    //    list.ForEach(p =>
                    //    {
                    //        p.IsRecommend = !p.IsRecommend;
                    //    });
                    //    break;
                }
                bool isSuccess = true;
                try
                {
                    dc.SubmitChanges();
                }
                catch (Exception ex)
                {
                    isSuccess = false;
                    throw SiteInfo.WarnFactory.CreateWarn(ex);
                }
                string msgKey = "操作";
                if (isSuccess)
                    WarnFactory.CreateWarn(string.Format("{0}成功", msgKey), WarnIcon.Success, WarnLink.Current).Alert();
            }
        }
    }


}
